using System;
using System.Threading.Tasks;
using JlPay.Sdk.Core;
using JlPay.Sdk.Services.Openmerch.Device;
using JlPay.Sdk.Models.Openmerch.Device;

namespace JlPay.Sdk.Examples.Openmerch.Device.DeviceQuery
{
    /// <summary>
    /// 终端查询使用示例
    /// @author liaochangyi
    /// @since 2025-01-25
    /// </summary>
    public class DeviceQueryDemo
    {
        /// <summary>
        /// 主程序入口
        /// </summary>
        /// <param name="args">命令行参数</param>
        /// <returns>任务</returns>
        public static async Task Main(string[] args)
        {
            Console.WriteLine("==========================================");
            Console.WriteLine("===== 终端查询示例 开始 =====");
            Console.WriteLine("==========================================");
            Console.WriteLine();

            await DeviceQueryDemoAsync();

            Console.WriteLine("==========================================");
            Console.WriteLine("===== 终端查询示例执行完毕 =====");
            Console.WriteLine("==========================================");
        }

        /// <summary>
        /// 创建配置对象
        /// </summary>
        /// <returns>配置对象</returns>
        private static Config CreateConfig()
        {
            return DemoConfig.CreateConfig();
        }

        /// <summary>
        /// 构建设备查询请求
        /// </summary>
        /// <returns>请求对象</returns>
        private static DeviceQueryRequest BuildDeviceQueryRequest()
        {
            var request = new DeviceQueryRequest
            {
                MerchNo = "84944035812S123",
                // 可选参数，注释掉表示查询该商户下所有终端
                // DeviceNo = "AQR1000030",
                // TermNo = "AQR1000030"
            };

            return request;
        }

        /// <summary>
        /// 终端查询示例
        /// </summary>
        public static async Task DeviceQueryDemoAsync()
        {
            Console.WriteLine("开始执行终端查询示例");
            
            try
            {
                // 创建配置对象和设备管理服务
                var config = CreateConfig();
                var deviceService = new DeviceService(config);
                
                // 创建终端查询请求
                var request = BuildDeviceQueryRequest();
                
                // 发送请求
                var response = await deviceService.DeviceQueryAsync(request);
                
                // 处理响应结果
                response.HandleResponse("终端查询", config);
            }
            catch (Exception ex)
            {
                var config = CreateConfig();
                var response = new DeviceQueryResponse();
                response.HandleResponseWithException("终端查询", config, ex);
            }
            
            Console.WriteLine("终端查询示例执行完毕");
        }
    }
} 